<?php


namespace app\nft\controller;

use app\BaseController;
use app\model\Nft;
use think\Exception;
use think\facade\Db;
use think\Request;

class Index extends BaseController
{
    public function index(Request $request){

        $keyword = $request->param('NftKeyword');

        if (null != $keyword){
            $rs = Nft::where('nftname','like','%'.$keyword.'%')
                ->where('pass',1)
            ->paginate([15,false,'query'=>$request->param()]);
            //分页带参数，TP5的区别paginate(15,false,['query'=>request()->param()])
        }else{
            $rs = Db::name('nft')->where('pass',1)->paginate(15)->order('id','desc');
        }

        $page = $rs->render();
        $count = $rs->total();

        return view('index',['rs'=>$rs,'total'=>$count,'page'=>$page]);


    }

    //编辑渲染
    public function edit(Request $request){
        $id = $request->param('id');
        //获取待修改数据ID并开始执行查询
        $res = Nft::where('id',$id)->find();
        if (empty($res)){
            return 'err';
        }
        return view('edit',['rs'=>$res]);
    }

    //保存更改
    public function save(Request $request){
        $data = $request->param();


        try {
            //判断是否有文件上传
            $file = $request->file('nftlogo');
            if (is_null($file)) {
                $data['nftlogo'] = $data['nftlogo_temp'];
            }else{

                try {
                    validate(['nftlogo'=>[
                        'fileSize' => 102400,
                        'fileExt' => 'jpg,jpeg,png,bmp,gif',
                        'fileMime' => 'image/jpeg,image/png,image/gif', //这个一定要加上，很重要我认为！
                    ]])->check(['nftlogo' => $file]);
                    //官方文档中check($file)报错参数为数组
                    $savename = \think\facade\Filesystem::disk('public')->putFile( '', $file);
                    $data['nftlogo'] = $savename;
                } catch (\think\exception\ValidateException $e) {
                    echo $e->getMessage();
                }

            }

        }catch (Exception $e){
            $data['nftlogo'] = $data['nftlogo_temp'];
        }

        $res = Nft::where('id',$data['id'])->find();
        if (empty($res)){
            return 'err';
        }
        $setData = Nft::update($data);
        //return view('edit',['rs'=>$res,'success'=>'Done']);
        //return redirect('/nft/edit/'.$data['id']);
        return "<a href=\"#\" onClick=\"javascript:window.history.go(-2);return false;\">成功，返回</a>";

    }



    //渲染添加NFT界面
    public function addNftShow(){
        return view('addnftshow');
    }


    //添加NFT数据
    public function saveAddNft(Request $request){

        $data = $request->param();

        if($data['nftname']!=''){

            try {
                //判断是否有文件上传
                $file = $request->file('nftlogo');
                if (is_null($file)) {
                    $data['nftlogo'] = '';
                }else{
                    try {
                        validate(['nftlogo'=>[
                            'fileSize' => 102400,
                            'fileExt' => 'jpg,jpeg,png,bmp,gif',
                            'fileMime' => 'image/jpeg,image/png,image/gif', //这个一定要加上，很重要我认为！
                        ]])->check(['nftlogo' => $file]);
                        //官方文档中check($file)报错参数为数组
                        $savename = \think\facade\Filesystem::disk('public')->putFile( '', $file);
                        $data['nftlogo'] = $savename;
                    } catch (\think\exception\ValidateException $e) {
                        echo $e->getMessage();
                    }

                }

            }catch (Exception $e){
                $data['nftlogo'] = '';
            }

            $res = Nft::create($data);
        }else{
            echo '<script>alert("项目名称不能为空");window.history.go(-1);</script>';
            return;
        }

        //return "<a href=\"#\" onClick=\"javascript:window.history.go(-1);return false;\">成功，返回</a>";
        echo '<script>alert("添加成功");window.history.go(-2);</script>';


    }

    //TODO 新增数据+审核；搜索；多人协同;表单时间选择;找个后台管理界面，把添加、审核和编辑丢进去



}